<template>
  <!-- 分类参数列表 -->
  <div class="goodsList">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="handleClearAble"
          >
            <el-button slot="append" icon="el-icon-search" @click="queryGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsListTable" border stripe>
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column label="商品 ID" prop="goods_id" align="center"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          align="center"
          width="500"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="重量" prop="goods_weight" align="center"></el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{scope.row.add_time | format}}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoodsById(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 编辑商品列表的对话框 -->
      <el-dialog
        title="编辑商品"
        width="50%"
        :visible.sync="editGoodsDialogVisible"
        @close="handleDialogClose"
      >
        <!-- 表单区域 -->
        <el-form
          :model="editDialogForm"
          :rules="editDialogFormRules"
          ref="editDialogFormRef"
          label-width="100px"
        >
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editDialogForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input v-model="editDialogForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="editDialogForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="editDialogForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <!-- 富文本编辑器 -->
            <quill-editor v-model="editDialogForm.goods_introduce"></quill-editor>
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="sureEditGoods">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询信息对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 20
      },
      // 商品列表数组
      goodsListTable: [],
      // 商品列表总数
      total: 0,
      // 控制编辑商品对话框的显示和隐藏
      editGoodsDialogVisible: false,
      editDialogForm: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: ''
      },
      editDialogFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$axios.get('goods', {
        params: { ...this.queryInfo }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户列表数据失败')
      }
      // 赋值给用户列表
      this.goodsListTable = res.data.goods
      // 获取总页数
      this.total = res.data.total
    },
    // 清空搜素框
    handleClearAble() {
      this.queryInfo.pagenum = 1
      this.getGoodsList()
    },
    // 查询商品
    queryGoodsList() {
      this.getGoodsList()
    },
    addGoods() {
      this.$router.replace('/goods/add')
    },
    async editGoodsById(id) {
      const { data: res } = await this.$axios.get(`goods/${id}`)
      // 获取商品数据失败
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败！！！')
      }
      // 获取成功
      this.editDialogForm = res.data
      console.log(res.data)
      // 显示对话框
      this.editGoodsDialogVisible = true
    },
    async removeGoodsById(id) {
      // 确认是否删除
      const confirmResult = await this.MessageBox.confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.Notification.info({
          title: '消息',
          message: '取消了删除操作'
        })
      }

      // 执行操作步骤
      const { data: res } = await this.$axios.delete(`goods/${id}`)

      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！！！')
      }

      // 提示删除成功
      this.$message({
        type: 'success',
        message: res.meta.msg
      })
      // 刷新用户列表
      this.getGoodsList()
    },
    // 监听 页数大小变化
    handleSizeChange(newSize) {
      this.queryInfo.pagenum = 1
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听 页码变化
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 监听对话框关闭
    handleDialogClose() {
      this.$refs['editDialogFormRef'].resetFields()
    },
    sureEditGoods() {
      // 表单预验证
      this.$refs['editDialogFormRef'].validate(async valid => {
        if (!valid) {
          return
        }
        // 提交表单
        const { data: res } = await this.$axios.put(
          `goods/${this.editDialogForm.goods_id}`,
          {
            goods_cat: this.editDialogForm.goods_cat,
            goods_name: this.editDialogForm.goods_name,
            goods_price: this.editDialogForm.goods_price,
            goods_number: this.editDialogForm.goods_number,
            goods_weight: this.editDialogForm.goods_weight,
            goods_introduce: this.editDialogForm.goods_introduce
          }
        )
        // 编辑失败
        if (res.meta.status !== 200) {
          return this.$message.error('编辑商品数据失败!!!')
        }
        // 编辑成功
        this.$message({
          type: 'success',
          message: '编辑成功'
        })
        // 关闭对话框
        this.editGoodsDialogVisible = false
        // 刷新列表
        this.getGoodsList()
      })
    }
  }
}
</script>
<style lang="less">
.goodsList {
  .ql-container {
    height: 120px;
  }
}

.el-cascader-panel {
  height: 200px;
}
.goods-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.goods-uploader .el-upload:hover {
  border-color: #409eff;
}
.goods-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.goods_pic {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
